package com.scalar.chiptrack.finance.openpo.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.openpo.actioncommands.PurchaseOrderSearchActionCommandFactory;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Author: Phani Raja Kumar
 * Date  : Feb 18, 2005
 * Time  : 2:00:19 PM
 */
public class PurchaseOrderSearchAction extends ChipTrackExtendedAction {

    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {
		HashMap errors  = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info  	= ( HashMap ) request.getAttribute( INFO_MESSAGES );
		
		if( errors == null )
		{
			errors  = new HashMap();
		}
		if( info == null )
		{
			info  = new HashMap();
		}

        String action   = request.getParameter( REQUEST_ACTION );
        ActionForward actionForward = mapping.findForward( "search" );
        ChipTrackExtendedAction purchaseOrderCommand = null;

        //Checking If there is any action specified else redirecting to the search page with out processing any further
        if ( ( action == null ) || ( action.trim().length() == 0 ) )
        {
            //No action is specified
            actionForward = mapping.findForward( "search" );
        }
        else
        {
			purchaseOrderCommand = PurchaseOrderSearchActionCommandFactory.getActionCommand( action );
            if ( purchaseOrderCommand == null )
            {
                Logger.error( ERROR_COULD_NOT_CREATE_PURCHASE_ORDER_SEARCH_INSTANCE );
                errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_CREATE_PURCHASE_ORDER_SEARCH_INSTANCE );
                actionForward = mapping.findForward( "search" );
            }
            else
            {
                try
                {
                    actionForward = purchaseOrderCommand.execute( mapping, form, request, response );
					errors  = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
					info  	= ( HashMap ) request.getAttribute( INFO_MESSAGES );
                }
                catch ( Exception e )
                {
                    Logger.error( ERROR_WHILE_EXECUTING_PURCHASE_ORDER_SEARCH_ACTION_EXECUTE, e );
                    errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_WHILE_EXECUTING_PURCHASE_ORDER_SEARCH_ACTION_EXECUTE );

                    pSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
                    actionForward = mapping.findForward( "search" );
                }
            }
        }
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
		request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        return actionForward;
    }
}
